#标准养护任务，用于构成养护任务模板
class StandardmaintenanceController < ApplicationController
  before_filter :sys_before_log

  #获取指定模板的任务列表
  def getStandardMaintenances
    standardMaintenances = StandardMaintenance.find_all_by_template_id params[:templateId]
    total = standardMaintenances.length
    jsonStr="{standardMaintenance:"+standardMaintenances.to_json(:include=>[:team,:work])+",total:"+total.to_s+"}"
    render :text => jsonStr,:layout => false
  end

  def create
    begin
      standardMaintenance = StandardMaintenance.new
      standardMaintenance.work_id = params[:workId]
      standardMaintenance.team_id = params[:teamId]
      standardMaintenance.template_id = params[:templateId]
      standardMaintenance.begin_minute = params[:begin_minute].to_s
      standardMaintenance.during_minute = params[:during_minute].to_s
      standardMaintenance.dynamic_during_minute = params[:dynamic_during_minute].to_s
      standardMaintenance.description = params[:description]
      standardMaintenance.save!
      info = 'success'
      
    rescue Exception => e
      logger.info e
      info = '创建异常'
    end

    result = "{success:true,info:\"#{info}\"}"
    render :text => result,:layout => false
  end

  def delete
    begin
      ids= params[:id][1..params[:id].length-2].split(',')
      StandardMaintenance.destroy(ids)
      info = 'success'
    rescue Exception=>e
      logger.info e
      info = '删除异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #获取指定标准任务的信息
  def edit
    standard_maintenance = StandardMaintenance.find(params[:id])
    json_str = standard_maintenance.to_json
    render :text=>json_str,:layout=>false
  end

  def update
    begin
      standardMaintenance = StandardMaintenance.find(params[:id])
      standardMaintenance.work_id = params[:workId]
      standardMaintenance.team_id = params[:teamId]
      standardMaintenance.begin_minute = params[:begin_minute]
      standardMaintenance.during_minute = params[:during_minute]
      standardMaintenance.dynamic_during_minute = params[:dynamic_during_minute]
      standardMaintenance.description = params[:description]
      standardMaintenance.save!
      info = 'success'
    rescue Exception=>e
      logger.info e
      info = '更新异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  def assign
    begin
      standardmaintenance = StandardMaintenance.find_by_id(params[:id])
      standardmaintenance.flag = params[:type]
      standardmaintenance.save!
      info = 'success'
    rescue Exception=>e
      logger.info e
      info = '操作异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

end
